﻿using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.SocketInterfaces;

namespace Gadgeteer.Modules.GHIElectronics
{
    // -- CHANGE FOR MICRO FRAMEWORK 4.2 --
    // If you want to use Serial, SPI, or DaisyLink (which includes GTI.SoftwareI2CBus), you must do a few more steps
    // since these have been moved to separate assemblies for NETMF 4.2 (to reduce the minimum memory footprint of Gadgeteer)
    // 1) add a reference to the assembly (named Gadgeteer.[interfacename])
    // 2) in GadgeteerHardware.xml, uncomment the lines under <Assemblies> so that end user apps using this module also add a reference.

    /// <summary>
    /// Represents a potentiometer module for Microsoft .Net Gadgeteer
    /// </summary>
    /// <example>
    /// <para>The following example uses a <see cref="Potentiometer"/> object to display the potentiometer position. 
    /// </para>
    /// <code>
    /// using System;
    /// using System.Collections;
    /// using System.Threading;
    /// using Microsoft.SPOT;
    /// using Microsoft.SPOT.Presentation;
    /// using Microsoft.SPOT.Presentation.Controls;
    /// using Microsoft.SPOT.Presentation.Media;
    /// using Microsoft.SPOT.Touch;
    ///
    /// using Gadgeteer.Networking;
    /// using GT = Gadgeteer;
    /// using GTM = Gadgeteer.Modules;
    /// using Gadgeteer.Modules.GHIElectronics;
    ///
    /// namespace TestApp
    /// {
    ///     public partial class Program
    ///     {
    ///         void ProgramStarted()
    ///         {
    ///             double potentiometerPercent = potentiometer.ReadPotentiometerPercentage();
    ///         }
    ///     }
    /// }
    /// </code>
    /// </example>
    public class Potentiometer : GTM.Module
    {
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The mainboard socket that has the module plugged into it.</param>
        public Potentiometer(int socketNumber)
        {
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);
            this.input = GTI.AnalogInputFactory.Create(socket, Socket.Pin.Three, this);
        }


        private GTI.AnalogInput input;

        /// <summary>
        /// Returns the current voltage reading of the potentiometer
        /// </summary>
        public double ReadPotentiometerVoltage()
        {
            return input.ReadVoltage();
        }

        /// <summary>
        ///  Returns the current position of the potentiometer relative to its maximum: range 0.0 to 1.0
        /// </summary>
        public double ReadPotentiometerPercentage()
        {
            return input.ReadProportion();
        }

    }
}
